/**
 * Place holder for copyright, needs to be replaced before release.
 */
package com.softao.ming.gwt.user.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasHTML;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Here are the style name used within this widget:
 * <ul>
 * <li>ming-DropdownLink: the root style
 * <li>ming-DropdownLinkHover; the style while this is hovered by mouse
 * <li>ming-DropdownLinkPanel: the popup panel style
 * </ul>
 */
public class DropdownLink extends Widget implements HasHTML {

	private Element mAnchorElem;

	private PopupPanel mPopupPanel;

	private Widget mPopupWidget;

	/**
	 * Initializes a new instance of <code>DropdownLink</code>.
	 */
	public DropdownLink() {
		SharedImages images = GWT.create(SharedImages.class);

		this.setElement(DOM.createDiv());
		DOM.appendChild(getElement(), mAnchorElem = DOM.createAnchor());
		DOM.appendChild(getElement(), images.gifArrowDown().createImage()
				.getElement());

		sinkEvents(Event.ONCLICK | Event.ONMOUSEOVER | Event.ONMOUSEOUT);
		setStyleName("ming-DropdownLink");
	}

	public String getHTML() {
		return DOM.getInnerHTML(mAnchorElem);
	}

	public void setHTML(String html) {
		DOM.setInnerHTML(mAnchorElem, html);
	}

	public String getText() {
		return DOM.getInnerText(mAnchorElem);
	}

	public void setText(String text) {
		DOM.setInnerText(mAnchorElem, text);
	}

	/**
	 * @return the popupWidget
	 */
	public Widget getPopupWidget() {
		return mPopupWidget;
	}

	/**
	 * @param popupWidget
	 *            the popupWidget to set
	 */
	public void setPopupWidget(Widget popupWidget) {
		mPopupWidget = popupWidget;
	}

	@Override
	public void onBrowserEvent(Event event) {
		if (DOM.eventGetType(event) == Event.ONCLICK) {
			if (this.mPopupWidget != null) {
				this.showPopupPanel();
			}
			DOM.eventPreventDefault(event);
		} else if (DOM.eventGetType(event) == Event.ONMOUSEOVER) {
			this.addStyleDependentName("Hover");
		} else if (DOM.eventGetType(event) == Event.ONMOUSEOUT) {
			this.removeStyleDependentName("Hover");
		}
	}

	private void showPopupPanel() {
		if (this.mPopupWidget == null) {
			return;
		}

		if (this.mPopupPanel == null) {
			this.mPopupPanel = new PopupPanel(true);
			this.mPopupPanel.setStyleName("ming-DropdownLinkPopup");
		}

		int left = this.getAbsoluteLeft();
		int top = this.getAbsoluteTop();
		int clientWidth = Window.getClientWidth();
		int clientHeight = Window.getClientHeight();
		this.mPopupPanel.clear();
		this.mPopupPanel.add(this.mPopupWidget);
		this.mPopupPanel.show();

		int offsetWidth = this.mPopupPanel.getOffsetWidth();
		int offsetHeight = this.mPopupPanel.getOffsetHeight();
		
		top = top + this.getOffsetHeight();
		if ((left + offsetWidth) > clientWidth) {
			left = left - (left + offsetWidth - clientWidth);
		}
		if ((top + offsetHeight) > clientHeight) {
			top = top - (top + offsetHeight - clientHeight);
		}

		this.mPopupPanel.setPopupPosition(left, top);
	}
}
